<style lang="less" scoped>
.pact-audit {
  width: 100%;
  height: 100%;
  /deep/.el-table__inner-wrapper {
    height: 100% !important;
  }
  .pagination-css {
    padding: 5px;
    box-sizing: border-box;
    background-color: #fff;
    display: flex;
    justify-content: right;
    margin-top: 5px;
  }
}
</style>
<template>
  <div class="pact-audit">
    <el-form :model="auditForm" inline class="demo-form-inline form-css">
      <el-form-item label="签约单位：">
        <!-- <el-input
          v-model="auditForm.contractingUnit"
          placeholder="请输入签约单位"
        /> -->
        <el-select v-model="auditForm.contractingUnit" filterable placeholder="请输入签约单位" style="width: 100%;">
          <el-option v-for="(item, index) in projectList" :key="index" :label="item.insname" :value="item.insname" />
        </el-select>
      </el-form-item>
      <el-form-item label="合同编号：">
        <el-input
          v-model="auditForm.contractingNumber"
          placeholder="请输入合同编号"
        />
      </el-form-item>
      <el-form-item label="">
        <el-button type="primary" @click="search"
          ><el-icon style="margin-right: 6px"><Search /></el-icon>
          搜索</el-button
        >
        <el-button @click="reset"
          ><el-icon style="margin-right: 6px"><Refresh /></el-icon>
          重置</el-button
        >
        <vue3-json-excel
          :fields="json_fields"
          :name="userExcelName"
          :fetch="downloadEx"
          style="margin-left: 12px"
        >
           <el-button>
            <!-- <el-icon style="margin-right: 6px"><Document /></el-icon> -->
            <img
              style="margin-right: 6px; width: 12px"
              src="@/images/daochu.png"
              alt=""
            />

            导出合同表</el-button
          >
        </vue3-json-excel>
      </el-form-item>
    </el-form>
    <div class="filter-column">
      <el-button  @click="addContracting"
        ><el-icon><Plus /></el-icon>新增合约</el-button
      >
    </div>
    <el-table
      v-loading="tableLoading"
      :data="tableData"
      :header-cell-style="{ backgroundColor: '#EEF6FF' }"
      stripe
      border
      height="calc(100% - 173px)"
      style="width: 100%; padding: 10px"
    >
      <el-table-column label="合同编号" align="center" prop="contractNo" />
      <el-table-column label="签约单位" align="center" prop="signIns" />
      <el-table-column label="签约时间" align="center" prop="signtm" />
      <el-table-column label="到期时间" align="center" prop="expiretm" />
      <el-table-column label="总合约量" align="center" prop="total" />
      <el-table-column label="已使用量" align="center" prop="usedcnt" />
      <el-table-column label="剩余量" align="center" prop="surplus" />
      <el-table-column label="销售省区" align="center" prop="addr" />
      <el-table-column label="销售姓名" align="center" prop="saler" />
      <el-table-column label="是否有效" align="center" prop="status">
        <template #default="{ row }">
          <span>{{ row.status == 0 ? '正常' : row.status == 100  ? '无效' : ''}}</span>
        </template>
      </el-table-column>
      <!-- <el-table-column label="备注" align="center" prop="notes" /> -->
      <el-table-column label="操作" align="center">
        <template #default="{ row }">
          <el-button type="primary" link @click="editContracting(row)"
            >编辑</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      v-model:current-page="pageList.current"
      v-model:page-size="pageList.size"
      :page-sizes="[20, 40, 60, 80]"
      small="small"
      layout="total, sizes, prev, pager, next, jumper"
      :total="pageList.total"
      class="pagination-css"
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
    />
    <el-dialog
      v-model="auditVisible"
      width="700px"
      :title="auditTitle"
      :close-on-press-escape="false"
      :close-on-click-modal="false"
    >
      <el-form :model="ruleForm" inline>
        <el-form-item label="合约编号：">
          <el-input
            v-model="ruleForm.contractNumber"
            placeholder="请输入合约编号"
            style="width: 200px"
          />
        </el-form-item>
        <el-form-item label="总合约量：">
          <el-input
            v-model="ruleForm.contractTotal"
            placeholder="请输入总合约量"
            style="width: 200px"
          />
        </el-form-item>
        <div style="width: 100%;">
          <el-form-item label="签约单位：" style="width: 100%;">
            <el-select v-model="ruleForm.inscodes" filterable multiple placeholder="" style="width: 100%;">
              <el-option v-for="(item, index) in projectList" :key="index" :label="item.insname" :value="item.insname" />
            </el-select>
          </el-form-item>
        </div>
        <!-- <el-form-item v-if="auditTitle == '编辑合约'" label="当前单位：">
          <span style="display: inline-block; width: 200px">
            {{ ruleForm.signIns }}</span>
        </el-form-item> -->
        <el-form-item label="签约时间：">
          <el-date-picker
            v-model="ruleForm.signtm"
            type="date"
            placeholder="请选择签约时间"
            format="YYYY-MM-DD"
            style="width: 200px"
          />
        </el-form-item>
        <el-form-item label="到期时间：">
          <el-date-picker
            v-model="ruleForm.expiretm"
            type="date"
            placeholder="请选择签约时间"
            format="YYYY-MM-DD"
            style="width: 200px"
          />
        </el-form-item>
        <el-form-item label="销售省区：">
          <el-select
            v-model="ruleForm.addr"
            class="m-2"
            placeholder="请选择销售省区"
            style="width: 200px"
          >
            <el-option
              v-for="(item, index) in provinceList"
              :key="index"
              :label="item.label"
              :value="item.label"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="销售姓名：">
          <!-- <el-select v-model="ruleForm.saler" class="m-2" placeholder="请选择销售姓名" style="width: 200px;">
            <el-option
              v-for="(item, index) in userList"
              :key="index"
              :label="item.label"
              :value="item.label"
            />
          </el-select> -->
          <el-input
            v-model="ruleForm.saler"
            placeholder="请输入销售姓名"
            style="width: 200px"
          />
        </el-form-item>
        <el-form-item label="是否有效：">
          <el-select v-model="ruleForm.status" placeholder="" style="width: 200px">
            <el-option v-for="(item, index) in statusList" :key="index" :label="item.label" :value="item.value" />
          </el-select>
        </el-form-item>

        <!-- <el-form-item v-if="auditTitle == '编辑合约'" label="销售姓名：">
          <el-input v-model="ruleForm.saler" placeholder="请输入销售姓名" style="width: 200px;" />
        </el-form-item>
        
        <el-form-item v-if="auditTitle == '编辑合约'" label="销售编号：">
          <el-input v-model="ruleForm.contractNumber" placeholder="请输入销售编号" style="width: 200px;" />
        </el-form-item> -->
      </el-form>
      <template #footer>
        <el-button type="" @click="auditVisible = false">取消</el-button>
        <el-button type="primary" @click="submit">保存</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import areaData from "@/utils/area_value";
import { getContractList, addContractWeb } from "@/apis/pactManagement";
import format from "@/utils/format";
import { ElMessage } from "element-plus";
import { getSubIns, getAllSubIns } from "@/apis/hospitalManagement";
export default {
  data() {
    let _this = this;
    return {
      projectList: [],
      fullscreenLoading: false,
      json_fields: {
        序号: {
          field: "index",
          callback: (value) => {
            return Strgin(value);
          },
        },
        合同编号: {
          field: "contractNo",
          callback: (value) => {
            return Strgin(value);
          },
        },
        签约单位: {
          field: "signIns",
          callback: (value) => {
            return Strgin(value);
          },
        },
        签约时间: {
          field: "signtm",
          callback: (value) => {
            return Strgin(value);
          },
        },
        到期时间: {
          field: "expiretm",
          callback: (value) => {
            return Strgin(value);
          },
        },
        总合约量: {
          field: "total",
          callback: (value) => {
            return Strgin(value);
          },
        },
        已使用量: {
          field: "usedcnt",
          callback: (value) => {
            return Strgin(value);
          },
        },
        剩余量: {
          field: "surplus",
          callback: (value) => {
            return Strgin(value);
          },
        },
        销售省区: {
          field: "addr",
          callback: (value) => {
            return Strgin(value);
          },
        },
        销售姓名: {
          field: "saler",
          callback: (value) => {
            return Strgin(value);
          },
        },
        备注: {
          field: "notes",
          callback: (value) => {
            return Strgin(value);
          },
        },
      },
      userExcelName: "",
      // hosProps: {
      //   lazy: true,
      //   checkStrictly: true,
      //   multiple: true,
      //   label: "insname",
      //   value: "inscode",
      //   lazyLoad(node, resolve) {
      //     _this.getJunior(node, resolve);
      //   },
      // },
      tableLoading: false,
      tableData: [],
      pageList: {
        current: 1,
        size: 20,
        total: 0,
      },
      auditVisible: false,
      auditTitle: "新增合约",
      auditForm: {
        contractingUnit: "",
        contractingNumber: "",
      },
      ruleForm: {
        contractNumber: "",
        contractTotal: "",
        signIns: "",
        inscodes: [],
        signtm: "",
        expiretm: "",
        addr: "",
        saler: "",
        id: "",
        status: ''
      },
      statusList: [{
        label: '正常',
        value: 0
      }, {
        label: '无效',
        value: 100
      }],
      provinceList: areaData,
      userList: [],
    };
  },
  mounted() {
    this.getJunior()
    this.init();
  },
  methods: {
    getJunior() {
      this.projectList = []
      getAllSubIns({
        uname: this.$store.getters.name,
        inscode: this.$store.getters.inscode,
        subins: '',
      }).then((res) => {
        if (res.code == 0) {
          if (res.data && res.data.length > 0) {
            this.projectList = res.data
          }
        }
      });
    },
    async downloadEx() {
      let data = [];
      this.fullscreenLoading = true;
      this.userExcelName = "超声报告服务审核类合约记录表";
      let res = await getContractList({
        uname: this.$store.getters.name,
        inscode: this.$store.getters.inscode,
        reportStatus: "audit",
      });
      if (res.code == 0) {
        if (res.data && res.data.length > 0) {
          res.data.forEach((el, index) => {
            el.index = index + 1;
            el.signtm = format(el.signtm * 1000, "YYYY-MM-DD");
            el.expiretm = format(el.expiretm * 1000, "YYYY-MM-DD");
            el.surplus =
              (el.total ? el.total : 0) - (el.usedcnt ? el.usedcnt : 0);
          });
          data = res.data;
        }
      }
      this.fullscreenLoading = false;
      return data;
    },
    // getJunior(node, resolve) {
    //   let subins = "";
    //   if (node.value) {
    //     subins = node.value;
    //   }
    //   getSubIns({
    //     uname: this.$store.getters.name,
    //     inscode: this.$store.getters.inscode,
    //     subins: subins,
    //   }).then((res) => {
    //     if (res.code == 0) {
    //       if (res.data && res.data.length > 0) {
    //         resolve(res.data);
    //       } else {
    //         resolve([]);
    //       }
    //     } else {
    //       resolve([]);
    //     }
    //   });
    // },
    init() {
      this.pageList.current = 1;
      this.getTableData();
    },
    getTableData() {
      this.tableLoading = true;
      this.tableData = [];
      getContractList({
        uname: this.$store.getters.name,
        inscode: this.$store.getters.inscode,
        reportStatus: "audit",
        signIns: this.auditForm.contractingUnit,
        contractNo: this.auditForm.contractingNumber,
      }).then((res) => {
        if (res.code == 0) {
          if (res.data && res.data.length > 0) {
            res.data.forEach((el) => {
              el.signtm = format(el.signtm * 1000, "YYYY-MM-DD");
              el.expiretm = format(el.expiretm * 1000, "YYYY-MM-DD");
              el.surplus =
                (el.total ? el.total : 0) - (el.usedcnt ? el.usedcnt : 0);
            });
            this.tableData = res.data;
          }
          this.pageList.total = res.size;
          this.tableLoading = false;
        }
      });
    },
    search() {
      this.pageList.current = 1;
      this.getTableData();
    },
    reset() {
      this.pageList.current = 1;
      this.auditForm.contractingUnit = "";
      this.auditForm.contractingNumber = "";
      this.getTableData();
    },
    addContracting() {
      this.auditTitle = "新增合约";
      this.ruleForm.contractNumber = "";
      this.ruleForm.contractTotal = "";
      this.ruleForm.signIns = "";
      this.ruleForm.inscodes = [];
      this.ruleForm.signtm = "";
      this.ruleForm.expiretm = "";
      this.ruleForm.addr = "";
      this.ruleForm.saler = "";
      this.ruleForm.id = "";
      this.ruleForm.status = "";
      this.auditVisible = true;
    },
    editContracting(row) {
      console.log(row, "row");
      let inscodes = []
      let arrayCode = []
      arrayCode = row.inscodes.split(',')
      if (!arrayCode[0]) {
        arrayCode = arrayCode.slice(1, -1)
      }
      inscodes = arrayCode
      this.ruleForm.contractNumber = row.contractNo;
      this.ruleForm.contractTotal = row.total;
      this.ruleForm.signIns = row.signIns;
      this.ruleForm.inscodes = inscodes
      this.ruleForm.signtm = row.signtm;
      this.ruleForm.expiretm = row.expiretm;
      this.ruleForm.addr = row.addr;
      this.ruleForm.saler = row.saler;
      this.ruleForm.id = row.id;
      this.ruleForm.status = row.status;
      this.auditTitle = "编辑合约";
      this.auditVisible = true;
    },
    submit() {
      let nowDate = new Date().getTime() / 1000;
      let data = {};
      if (this.auditTitle == "新增合约") {
        if (this.ruleForm.inscodes.length > 0) {
          let inscodes = []
          let signIns = []
          this.ruleForm.inscodes.forEach(el => {
            let insInfo = this.projectList.filter(item => {
              return item.insname == el
            })
            if (insInfo && insInfo.length > 0) {
              inscodes.push(insInfo[0].inscode)
              signIns.push(insInfo[0].insname)
            }
          })
          data = {
            addr: this.ruleForm.addr,
            contractNo: this.ruleForm.contractNumber,
            ctime: nowDate,
            devname: "",
            expiretm: this.ruleForm.expiretm
              ? new Date(this.ruleForm.expiretm).getTime() / 1000
              : "",
            id: this.ruleForm.id,
            info: "",
            inscode: this.$store.getters.inscode,
            inscodes: signIns.join(','),
            insname: this.$store.getters.insname,
            mtime: "",
            notes: "",
            operation: "",
            project: "",
            region: "",
            saler: this.ruleForm.saler,
            signIns: signIns.join(','),
            signtm: this.ruleForm.signtm
              ? new Date(this.ruleForm.signtm).getTime() / 1000
              : "",
            total: this.ruleForm.contractTotal,
            uname: this.$store.getters.name,
            usedcnt: 0,
            audittype: 'audit',
            status: this.ruleForm.status
          };
        } else {
          ElMessage({
            message: "请选择签约单位",
            type: "warning",
          });
        }
      } else {
        let inscodes = []
        let signIns = []
        this.ruleForm.inscodes.forEach(el => {
          let insInfo = this.projectList.filter(item => {
            return item.insname == el
          })
          if (insInfo && insInfo.length > 0) {
            inscodes.push(insInfo[0].inscode)
            signIns.push(insInfo[0].insname)
          }
        })
        data = {
          addr: this.ruleForm.addr,
          contractNo: this.ruleForm.contractNumber,
          mtime: nowDate,
          inscodes: signIns.join(','),
          signIns: signIns.join(','),
          expiretm: this.ruleForm.expiretm
            ? new Date(this.ruleForm.expiretm).getTime() / 1000
            : "",
          id: this.ruleForm.id,
          saler: this.ruleForm.saler,
          signtm: this.ruleForm.signtm
            ? new Date(this.ruleForm.signtm).getTime() / 1000
            : "",
          total: this.ruleForm.contractTotal,
          usedcnt: 0,
          audittype: 'audit',
          status: this.ruleForm.status
        };
      }
      addContractWeb({
        inscode: this.$store.getters.inscode,
        uname: this.$store.getters.name,
        data: JSON.stringify(data),
      }).then((res) => {
        if (res.code == 0) {
          ElMessage({
            message: "保存成功",
            type: "success",
          });
          this.init();
          this.auditVisible = false;
        }
      });
    },
    handleSizeChange(val) {
      this.pageList.current = 1;
      this.pageList.size = val;
      this.getTableData();
    },
    handleCurrentChange(val) {
      this.pageList.current = val;
      this.getTableData();
    },
  },
};
</script>